package thread.threadlocal;

import java.util.concurrent.TimeUnit;

/**
 * 用jVisualVM查看内存
 * 虽然get, set 方法里面都会清除内存泄漏的数据,但是还是没法做到完全清除所有内存泄漏的数据
 * @author jiangwz
 * @create 2022/3/8.
 */
public class ThreadLocalMemoryLeakDemo {

    public static void main(String[] args) throws InterruptedException {
        TimeUnit.SECONDS.sleep(10);
        ThreadLocal<User> threadLocal = new ThreadLocal<>();
        try {
            threadLocal.set(new User());
            threadLocal.get();
        } finally {
//            threadLocal.remove();
        }
        // threadLocal 主动被回收,key 才能在下次gc时回收，但是没办法看到
        threadLocal = null;
        TimeUnit.MINUTES.sleep(5);
    }

    static class User {
        private byte[] datas = new byte[1024*1024*100 * 10];
    }

    private static ThreadLocal<User> threadLocal = new ThreadLocal<>();


}
